package org.jks.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jks.config.Config;
import org.jks.model.Article;
import org.jks.model.Category;
import org.jks.request.model.UIArticle;
import org.jks.service.impl.ArticleServiceImpl;
import org.jks.service.impl.CategoryServiceImpl;
import org.jks.util.HtmlUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/article")
public class ArticleController {
	@Resource(name = "ArticleServiceImpl")
	private ArticleServiceImpl articleImpl;
	@Resource(name = "CategoryServiceImpl")
	private CategoryServiceImpl categoryImpl;
	
	private static int PAGE_SIZE = 25;
	
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, HttpSession session,
			Map<String, Object> model) {
		int page = 1;
		long count = articleImpl.count();
		int total_page = (int)(count%PAGE_SIZE == 0 ? count/PAGE_SIZE : (count/PAGE_SIZE + 1));
		List<Article> articles = articleImpl.listpage(page, PAGE_SIZE);
		model.put("articles", articles);
		model.put("page_size", PAGE_SIZE);
		model.put("total_page", total_page);
		model.put("current_page", page);
		Config.init(request, response, session, model, 0);
		return new ModelAndView("article/list", model);
	}
	
	@RequestMapping("/listpage/{page}")
	public ModelAndView listpage(@PathVariable int page, HttpServletRequest request,
			HttpServletResponse response, HttpSession session,
			Map<String, Object> model) {
		long count = articleImpl.count();
		int total_page = (int)(count%PAGE_SIZE == 0 ? count/PAGE_SIZE : (count/PAGE_SIZE + 1));
		List<Article> articles = articleImpl.listpage(page, PAGE_SIZE);
		model.put("articles", articles);
		model.put("page_size", PAGE_SIZE);
		model.put("total_page", total_page);
		model.put("current_page", page);
		Config.init(request, response, session, model, 0);
		return new ModelAndView("article/list", model);
	}
	
	@RequestMapping("/{index}/{categoryid}")
	public ModelAndView listpageByCategory(@PathVariable int index, @PathVariable int categoryid, HttpServletRequest request,
			HttpServletResponse response, HttpSession session,
			Map<String, Object> model) {
		int page = 1;
		long count = articleImpl.count();
		int total_page = (int)(count%PAGE_SIZE == 0 ? count/PAGE_SIZE : (count/PAGE_SIZE + 1));
		List<Article> articles = articleImpl.listpage(page, PAGE_SIZE, categoryid);
		model.put("articles", articles);
		model.put("page_size", PAGE_SIZE);
		model.put("total_page", total_page);
		model.put("current_page", page);
		model.put("category_id", categoryid);
		Config.init(request, response, session, model, index);
		return new ModelAndView("article/index", model);
	}
	
	@RequestMapping("/detail/{categoryid}/{id}")
	public ModelAndView detail(@PathVariable long categoryid, @PathVariable long id, HttpServletRequest request,
			HttpServletResponse response, HttpSession session,
			Map<String, Object> model) {
		Article article = articleImpl.findById(id);
		model.put("article", article);
		Config.init(request, response, session, model, Config.getSelectIndexByCategoryId(categoryid));
		return new ModelAndView("article/detail", model);
	} 

	@RequestMapping("/add")
	public ModelAndView add(UIArticle uiArticle, HttpServletRequest request,
			HttpServletResponse response, HttpSession session,
			Map<String, Object> model) {
		if (uiArticle.getContent() != null && uiArticle.getTitle() != null
				&& uiArticle.getTitle().trim().length() > 0
				&& uiArticle.getContent().trim().length() > 0) {
			Article article = new Article();
			Category category = categoryImpl.findById(uiArticle.getCategory());
			article.setCategory(category);
			Date now = new Date();
			article.setTitle(uiArticle.getTitle());
			article.setContent(uiArticle.getContent());
			
			article.setCreatetime(now);
			article.setModifytime(now);
			article.setBrief(HtmlUtil.getBrief(article.getContent()));
			model.put("article", article);
			articleImpl.save(article);
			Config.init(request, response, session, model, 0);
			return new ModelAndView("article/detail", model);
		}				
		List<Category> categories = categoryImpl.all();
		model.put("categories", categories);
		Config.init(request, response, session, model, 0);
		return new ModelAndView("article/add", model);
		
	}
	
}
